package com.apesay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apesay.exception.ServiceException;
import com.apesay.service.auth.RoleService;
import com.apesay.utils.Constant;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController extends RestController{
	@Autowired
	private RoleService roleService;
	@RequiresAuthentication
	@RequestMapping("manage")
	public String manage(){
		return "role/manage";
	}
	@RequiresAuthentication
	@RequestMapping("add")
	public String add(){
		return "role/add";
	}
	@RequiresAuthentication
	@RequestMapping("edit")
	public String edit(String objectId,Model model){
		model.addAttribute(Constant.OBJECTID, objectId);
		return "role/add";
	}
	@RequiresAuthentication
	@RequestMapping("customsave")
	@ResponseBody
	public JSONObject save(@RequestBody String body){
		JSONObject object=JSON.parseObject(body);
		try {
			return roleService.createRole(object);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequiresAuthentication
	@RequestMapping("customupdate/{id}")
	@ResponseBody
	public JSONObject customupdate(@PathVariable String id,@RequestBody String body){
		JSONObject object=JSON.parseObject(body);
		try {
			return roleService.updateRole(id, object);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	@Override
	protected String getModel() {
		return Constant.model_backend_role;
	}
}
